<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>日报文章</title>
	<#include "../common/header.ftl" >

    <script src="/static/jquery/jquery-form/jquery.form.js"></script>
    <script src="/static/jquery/jquery-bootstrap/jquery.bootstrap.min.js"></script>
    <script src="/static/My97DatePicker/WdatePicker.js"></script>
    <script src="/static/jquery/ckeditor/ckeditor.js"></script>
    <script src="/static/My97DatePicker/WdatePicker.js"></script>


    <style>
        .modal-body img {
            width: 100%;
        }
    </style>


    <script>
        $(function () {
            //添加触发模态框
            $('#addStrategyBtn').click(function () {
                //清空回显
                $('#newsPageModal input').val('');
                ck.setData("");
                $('#newsPageModal .btn-changePic').val('更换图片');
                $('#newsPageModal [name=releaseTime]').val('');
                $('#newsPageModal [name="state"]').val('');
                $('#newsPageModal #modal-coverUrl').remove();
                $('#newsPageModal [name="coverUrl"]').before('<img width="200px" id="modal-coverUrl"/>');
                $('#newsPageModal').modal('show');
            });

            //修改功能
            $('.btn-recomment').click(function () {
                var json = $(this).data('json');

                $('#newsPageModal #id').val(json.id);
                $('#newsPageModal [name="title"]').val(json.title);
                $('#newsPageModal [name="subTitle"]').val(json.subTitle);
                $('#newsPageModal [name="state"]').val(json.state);
                $('#newsPageModal [name="coverUrl"]').val(json.coverUrl);
                $('#newsPageModal #modal-coverUrl').attr('src',json.coverUrl);
                $('#newsPageModal [name=releaseTime]').val(json.releaseTime);



                //发请求查询相应的文章内容
                $.get('/newsPage/getContentById.do',{id:json.id},function (data) {
                    //回显文本内容
                    ck.setData(data.content);
                });

                $('#newsPageModal').modal('show');
            });

            //提交添加/修改表单
            $('#saveCommendBtn').click(function () {
                //获取富文本内容并设置到表单的文本域
                var data = ck.getData();
                $('#editor').html(data);
                //提交表单
                $('#commendForm').ajaxSubmit(function (data) {
                    if (data.success) {
                        $.messager.alert('温馨提示', '操作成功!  2s后刷新页面');
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                });
            });

            $('#strategySelect').change(function () {
                //获取攻略id
                var id = $(this).val();
                if(id!=-1){
                    //发请求查询其攻略分类
                    $.get('/strategyCatalog/getByStrategyId.do',{strategyId:id},function (data) {
                        var temp="";
                        $.each($(data),function (index,value) {
                            //拼接
                            temp +='<option value="'+ value.id+'">'+value.name+'</option>';
                        });
                        $('#strategyCatalogSelect').html(temp)
                    })
                }
            });

            //更换封面按钮触发文件上传框事件
            $('.btn-changePic').click(function () {
                //触发文件上传框
                $('#fileForm [name=file]').click();
            });

            //当文件上传框的值改变就发请求更新
            $('#fileForm').change(function () {
                //判断是否有上传文件
                if($('#fileForm [name=file]').val()){
                    //提交表单
                    $('#fileForm').ajaxSubmit(function (data) {
                        //根据返回结果判断上传结果
                        if(data.status==1){
                            $('#newsPageModal [name="coverUrl"]').val(data.url);
                            $('#newsPageModal #modal-coverUrl').attr('src',data.url);
                        }else{
                            $(document).dialog({
                                content:data.msg
                            })
                        }
                    })
                }
            })


            //查看文章
            $('.btn-read').click(function () {
                var id = $(this).data('id');
                $.get('/travel/read.do',{id:id},function (data) {
                    $('.content').html(data.content);
                });
                $('#travelModal').modal('show');
            })

        })
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
	<#include "../common/top.ftl" >
    <div class="row">
        <div class="col-sm-3">
            <!--设置用于菜单回显-->
                <#assign currentMenu="newsPage">
			<#include "../common/menu.ftl" >
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">日报管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/newsPage/list.do" method="post">
                <div class="form-group">
                    <input id="currentPage" type="hidden" name="currentPage" value="${(qo.currentPage)!}">
                    <label for="keyword">关键字:</label>
                    <input type="text" class="form-control" id="keyword" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题">
                </div>

                <input type="submit" id="btn_query" class="btn btn-success" value="查询">
                <input type="text" id="addStrategyBtn" class="btn btn-success" value="添加日报">

            </form>


            <table class="table table-striped table-hover">
                <thead>
                <tr style="align-content: center">
                    <th>标题</th>
                    <th>副标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>状态</th>

                </tr>
                </thead>
				<#list  pageInfo.list as newsPage>
					<tr style="align-content: center">
                        <td>${(newsPage.title)!}</td>
                        <td>${(newsPage.subTitle)!}</td>
                        <td>
                            <img src='${(newsPage.coverUrl)!}' width="50px">
                        </td>
                        <td>${(newsPage.releaseTime?string('yyyy-MM-dd '))!}</td>
                        <td>${(newsPage.stateName)!}</td>
                        <td>
                            <button class="btn btn-info btn-xs btn-recomment"
                                    data-json='${(newsPage.jsonString)!}'>
                                修改
                            </button>
                            <button class="btn btn-info btn-xs btn-read" data-id='${(newsPage.id)!}'>
                                查看文章
                            </button>

                        </td>
                    </tr>
                </#list>
            </table>

			<#include "../common/page.ftl" >
        </div>
    </div>
</div>


<#--推荐模态框-->
<div id="newsPageModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑/增加</h4>
            </div>
            <div class="modal-body">
                <form id="commendForm" class="form-horizontal" method="post" action="/newsPage/saveOrUpdate.do"
                      style="margin: -3px 118px">
                    <input id="id" type="hidden" name="id" value=""/>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">标题</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="title" placeholder="请输入标题">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">副标题</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="subTitle" placeholder="请输入副标题">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">封面</label>
                        <div class="col-sm-9">
                            <img width="200px" id="modal-coverUrl"/>
                            <input type="hidden" name="coverUrl"  >
                            <input TYPE="button" class="btn btn-info btn-xs btn-changePic" VALUE="更换图片" >
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">推荐时间</label>
                        <div class="col-sm-9">
                            <input type="text" name="releaseTime" class="form-control" onclick="WdatePicker()" readonly>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">状态</label>
                        <div class="col-sm-9">
                            <select class="form-control" autocomplete="off" name="state">
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                                <option value="-1">禁用</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <textarea id="editor" name="NewsContent.content" class="ckeditor" cols="10" rows="10">
                        </textarea>
                        <script>
                            var ck = CKEDITOR.replace('editor');
                        </script>
                    </div>
                </form>
                <#--隐藏的图片表单-->
                <form id="fileForm" action="/images.do" method="post" enctype="multipart/form-data">
                    <input type="file" class="form-control"  name="file" style="display: none">
                </form>
            </div>
            <div class="modal-footer">
                <a href="javascript:void(0);" class="btn btn-success" id="saveCommendBtn" aria-hidden="true">保存</a>
                <a href="javascript:void(0);" class="btn" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>


<div id="travelModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">查看内容</h4>
            </div>
            <div class="modal-body content">
            </div>
            <div class="modal-footer">
                <a href="javascript:void(0);" class="btn" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>

</body>
</html>